package com.ruoyi.manage.article.controller;

import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.manage.article.mapper.AuthInfoMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.manage.article.domain.Project;
import com.ruoyi.manage.article.service.IProjectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * projectController
 * 
 * @author ruoyi
 * @date 2024-11-21
 */
@RestController
@RequestMapping("/project/project")
public class ProjectController extends BaseController {
    @Autowired
    private IProjectService projectService;

    @Autowired
    private AuthInfoMapper authInfoMapper;

    /**
     * 查询project列表
     */
    @PreAuthorize("@ss.hasPermi('project:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(Project project) {
        startPage();
        List<Project> list = projectService.selectProjectList(project);


        for (Project project1 : list) {

            if (Objects.nonNull(project1.getIndustry())) {
                // 正则表达式，用于匹配逗号后的一位或多位数字
                String regex = ",(\\d+)"; // 注意这里的 \\d+ 表示一位或多位数字

                // 创建 Pattern 对象
                Pattern pattern = Pattern.compile(regex);

                // 创建 Matcher 对象
                Matcher matcher = pattern.matcher(project1.getIndustry());

                // 检查是否存在匹配
                if (matcher.find()) {
                    String industryName = authInfoMapper.getIndustryName(matcher.group(1));
                    project1.setIndustry(industryName);
                }
            }
        }

        return getDataTable(list);
    }

    /**
     * 导出project列表
     */
    @PreAuthorize("@ss.hasPermi('project:project:export')")
    @Log(title = "project", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Project project) {
        List<Project> list = projectService.selectProjectList(project);
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        util.exportExcel(response, list, "project数据");
    }

    /**
     * 获取project详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:project:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Project project = projectService.selectProjectById(id);

        Project instry = getInstry(project);
        return success(instry);
    }

    /**
     * 新增project
     */
    @PreAuthorize("@ss.hasPermi('project:project:add')")
    @Log(title = "project", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Project project) {
        return toAjax(projectService.insertProject(project));
    }

    /**
     * 修改project
     */
    @PreAuthorize("@ss.hasPermi('project:project:edit')")
    @Log(title = "project", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Project project) {

        return toAjax(projectService.updateProject(project));
    }

    /**
     * 删除project
     */
    @PreAuthorize("@ss.hasPermi('project:project:remove')")
    @Log(title = "project", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(projectService.deleteProjectByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('project:project:list')")
    @GetMapping("/count")
    public AjaxResult count() {
        return projectService.count();
    }

    public Project getInstry(Project project) {

        if (Objects.nonNull(project.getIndustry())) {
            // 正则表达式，用于匹配逗号后的一位或多位数字
            String regex = ",(\\d+)"; // 注意这里的 \\d+ 表示一位或多位数字

            // 创建 Pattern 对象
            Pattern pattern = Pattern.compile(regex);

            // 创建 Matcher 对象
            Matcher matcher = pattern.matcher(project.getIndustry());

            // 检查是否存在匹配
            if (matcher.find()) {
                String industryName = authInfoMapper.getIndustryName(matcher.group(1));
                project.setIndustry(industryName);
                return project;
            }
        }
        return project;
    }
}
